package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    // 查询通用设置
    public ResponseEntity findSettingVo() {
        // 1.获取线程内的userId
        Long userId = UserHolder.get().getId();
        // 2.根据userId查询陌生人问题
        Question question = questionService.findByUserId(userId);
        // 3.根据userId查询推送通知
        Notification notification = notificationService.findByUserId(userId);
        // 4.封装settingVo
        SettingVo settingVo = new SettingVo();
        // 4-1 问题
        if (question != null) {
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        // 4-2 推送通知
        if (notification != null) {
            settingVo.setLikeNotification(notification.getLikeNotification());
            settingVo.setPinglunNotification(notification.getPinglunNotification());
            settingVo.setGonggaoNotification(notification.getGonggaoNotification());
        }
        // 4-3 用户信息
        settingVo.setId(userId);
        settingVo.setPhone(UserHolder.get().getPhone());
        // 5.返回vo
        return ResponseEntity.ok(settingVo);
    }

    // 设置陌生人问题
    public void setQuestion(String content) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.查询此用户陌生人问题
        Question question = questionService.findByUserId(userId);
        if (question == null) {
            // 2-1 新增
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        } else {
            // 2-2 修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    // 设置推送通知
    public void setNotification(Notification param) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.根据userId查询推送通知
        Notification notification = notificationService.findByUserId(userId);
        if (notification == null) {
            // 2-1 新增
            param.setUserId(userId);
            notificationService.save(param);
        } else {
            // 2-2 修改
            notification.setPinglunNotification(param.getPinglunNotification());
            notification.setGonggaoNotification(param.getGonggaoNotification());
            notification.setLikeNotification(param.getLikeNotification());
            notificationService.update(notification);
        }
    }

    // 黑名单分页查询
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询黑名单分页
        PageBeanVo pageBeanVo = blackListService.findByPage(userId, pageNum, pageSize);
        // 3.将黑名单用户id转换为用户基本信息
        List<BlackList> blackLists = (List<BlackList>) pageBeanVo.getItems();
        List<UserInfo> userInfoList = new ArrayList<>();
        if (blackLists != null && blackLists.size() > 0) {
            for (BlackList blackList : blackLists) {
                // 取出黑名单id
                Long blackUserId = blackList.getBlackUserId();
                // 查询此用户信息
                UserInfo userInfo = userInfoService.findById(blackUserId);
                // 添加到集合中
                userInfoList.add(userInfo);
            }
        }
        // 4.将userInfo数据设置到分页对象
        pageBeanVo.setItems(userInfoList);
        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 移除黑名单
    public void removeBlackList(Long blackUserId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.根据userId和blackUserId删除黑名单
        blackListService.remove(userId, blackUserId);
    }
}
